﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System.Collections;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.Specialized;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    [DataContract]
    [Serializable]
#if NO_SILVERLIGHT
    [Browsable(true)]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [EditorBrowsable(EditorBrowsableState.Always)]
#endif
    public class Item : GameThingBaseClass, IFromTemplate
    {
#if NO_SILVERLIGHT
        [OnDeserialized]
        public void PostDeserialization(StreamingContext context)
        {
            PerformPostRestoreSetup();
        }
#endif

        public override void PerformPostRestoreSetup()
        {
            Register();
        }

        [NonSerialized, XmlIgnore]
        public ItemCollection _parentItemCollection;

        public override void Register()
        {
            base.Register();
        }

#if NO_SILVERLIGHT
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public long AssociatedWith
        {
            get { return _parentItemCollection == null ? 0 : _parentItemCollection.AssociatedWith; }
        }


        [EditorBrowsable(EditorBrowsableState.Advanced)]
        [XmlIgnore]
        public ItemCollection ParentItemCollection
        {
            get
            {
                if (_parentItemCollection == null)
                    Interlocked.CompareExchange<ItemCollection>(ref _parentItemCollection, new ItemCollection(AssociatedID), null);                    

                return _parentItemCollection;
            }
            set
            {
                _parentItemCollection = value;
            }
        }
    }
}
